<?php
session_start();

class User_model extends CI_Model {
	public function __construct()
	{
		 parent::__construct();
		 
	}
	function validateadmin()
	{
		$this->db->where('email', $this->input->post('email'));
		$this->db->where('password', md5($this->input->post('password')));
		$this->db->where('(groupid=1 or groupid=2)');
		
		$query = $this->db->get('users');
		
		if($query->num_rows == 1)
		{
			$loggedin=array("logged_in"=>1);
			$update_logged_in=$this->general_model->update('users',$loggedin,'email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");
			$userdetails=$this->general_model->select_val('users','*','activated = 1 and email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");
			$user_type =$userdetails['groupid'];
			
			$this->session->set_userdata('user_type',$user_type);
			$this->session->set_userdata("status","admin");		
			$this->session->set_userdata("adminlogged",$loggedin);
			$this->session->set_userdata("admin",$userdetails);
		
			return true;
		}
		
	}
	function validateuser()
	{
		$username="(email ='".$this->input->post('email')."' or username = '".$this->input->post('username')."')";
		$this->db->where($username);
		
		$this->db->where('password', md5($this->input->post('password')));
		$this->db->where('groupid',3);
		$this->db->where('activated',1);
		$query = $this->db->get('users');
		
		if($query->num_rows == 1)
		{
			
			$userlogintime=$this->general_model->select_val('users','*','email = "'.$this->input->post('email').'" or username = "'.$this->input->post('username').'" and password = "'.md5($this->input->post('password')).'"');
			if(strtotime($userlogintime['last_login']) < 0)
			{
				$last_login=date('Y-m-d H:i:s');
			}
			else{
				$last_login=$userlogintime['last_login'];
			}
			$loggedin=array("logged_in"=>1,
							"last_login"=>date('Y-m-d H:i:s'));
			$update_logged_in=$this->general_model->update('users',$loggedin,'email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");
			
			$userdetails=$this->general_model->select_val('users','*','email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");

            $user_type =$userdetails['groupid'];
  
			$this->session->set_userdata('user_type',$user_type);
			$_SESSION['username'] = $userdetails['firstname'];
			$_SESSION['userid'] = $userdetails['userid'];
			$this->session->set_userdata("status","user");			
			$this->session->set_userdata("userlogged",$loggedin);
			$this->session->set_userdata("user",$userdetails);
			$this->session->set_userdata("lastlogin",$last_login);
			return true;
		}
		
	}
	function fbloginuser($email)
	{
		$this->db->where('email', $email);
		$this->db->where('groupid',3);
		$this->db->where('activated',1);
		$query = $this->db->get('users');
		
		if($query->num_rows == 1)
		{
			
			$userlogintime=$this->general_model->select_val('users','*','email = '."'".$email."'");
			if(strtotime($userlogintime['last_login']) < 0)
			{
				$last_login=date('Y-m-d H:i:s');
			}
			else{
				$last_login=$userlogintime['last_login'];
			}
			$loggedin=array("logged_in"=>1,
							"last_login"=>date('Y-m-d H:i:s'));
			$update_logged_in=$this->general_model->update('users',$loggedin,'email = '."'".$email."'");
			
			$userdetails=$this->general_model->select_val('users','*','email = '."'".$email."'");

            $user_type =$userdetails['groupid'];
  
			$this->session->set_userdata('user_type',$user_type);
			$_SESSION['username'] = $userdetails['firstname'];
			$_SESSION['userid'] = $userdetails['userid'];
			$this->session->set_userdata("status","user");			
			$this->session->set_userdata("userlogged",$loggedin);
			$this->session->set_userdata("user",$userdetails);
			$this->session->set_userdata("lastlogin",$last_login);
			return true;
		}
	}
	function loggedin($user)
	{
		if($user==$this->session->userdata('status'))
		{
			$loggedin=1;
		}
		else 
		{
			$loggedin=0;
		}
		return (bool) $loggedin;
	}
	
	function logout()
	{
		$this->load->library('HybridAuthLib');
		$adapter = $this->hybridauthlib->getAdapter("Facebook");
		$adapter->logout();
		//$user=$this->session->userdata('status');
		
		//$this->general_model->update('users',array('logged_in'=>0),'userid = '.$user['userid']);
		$this->session->sess_destroy();
		
	}
}